package cn.edu.zjut.mina.util;



import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;

import cn.edu.zjut.mina.util.GsonUtils;
import cn.edu.zjut.mina.protocal.JMessageProtocalReq;
import cn.edu.zjut.mina.protocal.JMessageProtocalRes;
import cn.edu.zjut.mina.protocal.ProtocalRegulation;
import cn.edu.zjut.mina.bean.StandardFormat;
public class KeepAliveFactoryImpl implements KeepAliveMessageFactory{

	private static final String HEARTREQUEST="0x01";
	private static final String HEARTRESPONSE="0x02";
	public boolean isRequest(IoSession session, Object message) {
		// TODO Auto-generated method stub
		
		//System.out.println(message.toString().length()+"-----"+message);
		try {
			StandardFormat inJson = GsonUtils.jsonToBean(((JMessageProtocalReq) message).getContent(),
					StandardFormat.class);

			System.out.println("判断心跳请求:message-------"+message.toString()+"------"+inJson.heart);
			
			if (inJson.heart != null && inJson.heart.equals(HEARTREQUEST)) {
				System.out.println("调用isRequest()返回true:" + message.toString());
				return true;
			}
		} catch (Exception e) {
				System.out.println("格式不是req");
		}
		try{
			StandardFormat inJson = GsonUtils.jsonToBean(((JMessageProtocalRes) message).getContent(),
					StandardFormat.class);

			System.out.println("判断心跳请求:message(2)-------"+message.toString()+"------"+inJson.heart);
			
			if (inJson.heart != null && inJson.heart.equals(HEARTRESPONSE)) {
				System.out.println("调用isRequest()返回true:" + message.toString());
				return true;
			}
		}catch(Exception e){
			System.out.println("isRequest()产生异常");
		}
		return false;
	}

	public boolean isResponse(IoSession session, Object message) {
		// TODO Auto-generated method stub
		//System.out.println(message.toString().length()+"-----"+message);
		try {
			StandardFormat inJson = GsonUtils.jsonToBean(((JMessageProtocalReq) message).getContent(),
					StandardFormat.class);
		System.out.println("判断心跳响应:message-------"+message.toString()+"------"+inJson.heart);
			

			if (inJson.heart != null && inJson.heart.equals(HEARTREQUEST)) {
				System.out.println("调用isResponse()返回true:" + message.toString());
				return true;
			}
		} catch (Exception e) {
				System.out.println("调用isResponse()时抛出异常返回false:"+message.toString());
		}

		return false;
	}

	public Object getRequest(IoSession session) {
		// TODO Auto-generated method stub
		StandardFormat outJson=new StandardFormat();
		outJson.heart=HEARTRESPONSE;
		outJson.result=false;
		outJson.resultInfo=ResultInfoConstUtils.HEART_REQUEST;
		JMessageProtocalRes res=new JMessageProtocalRes();
		res.setResultCode(ProtocalRegulation.HEARTRESPONSE);
		String datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		System.out.println("调用getRequest()");
		return res;
	}

	public Object getResponse(IoSession session, Object request) {
		// TODO Auto-generated method stub
		StandardFormat outJson=new StandardFormat();
		outJson.heart=HEARTRESPONSE;
		outJson.result=true;
		outJson.resultInfo=ResultInfoConstUtils.HEART_REQUEST;
		JMessageProtocalRes res=new JMessageProtocalRes();
		res.setResultCode(ProtocalRegulation.HEARTRESPONSE);
		String datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		System.out.println("调用getResponse()");
		return res;
	}

}
